package com.crm.controller;

import com.crm.service.CustomerLossService;
import com.crm.service.CustomerOrderService;
import com.crm.service.CustomerService;
import com.crm.service.SaleChanceService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

/**
 * 页面跳转控制器
 */

@Controller
@RequestMapping("/page")
public class PageController {


    @Resource
    private SaleChanceService saleChanceService;
    @Resource
    private CustomerService customerService;
    @Resource
    private CustomerOrderService orderService;
    @Resource
    private CustomerLossService lossService;



    // 登录页面
    @GetMapping("/login")
    public String login() {
        return "login";
    }


    // 主页
    @GetMapping("/index")
    public String index() {
        return "index";
    }

    
    // 错误页面
    @GetMapping("/error/index")
    public String error(){
        return "error/index";
    }

    
    // 403 页面
    @GetMapping("/error/403")
    public String error403(){
        return "error/403";
    }


    // 404 页面
    @GetMapping("/error/404")
    public String error404(){
        return "error/404";
    }

    // 欢迎页面
    @GetMapping("/home")
    public String pageMain(HttpSession session, Model m) {
        ServletContext servletContext = session.getServletContext();
        Integer total = (Integer) servletContext.getAttribute("total");
        if(total == null){
            total = 1;
        }else{
            total += 1;
        }
        System.out.println(total);
        servletContext.setAttribute("total", total);
        m.addAttribute("countUser", customerService.userCount());
        m.addAttribute("countCustomer", customerService.customerCount());
        m.addAttribute("countDev", customerService.devCount());
        m.addAttribute("countLoss", customerService.lossCount());
        return "home/index";
    }


    // 用户页面
    @GetMapping("/user")
    public String user() {
        return "system/user/index";
    }


    // 修改用户信息页面
    @GetMapping("/update-profile")
    public String profile() {
        return "system/user/update-profile";
    }


    // 修改密码
    @GetMapping("/update-password")
    public String updPwd() {
        return "system/user/update-password";
    }


    // 角色页面
    @GetMapping("/role")
    public String role() {
        return "system/role/index";
    }


    // 菜单页面
    @GetMapping("/module")
    public String module() {
        return "system/module/index";
    }


    // 销售机会页面
    @GetMapping("/sale-chance")
    public String saleChance() {
        return "sale/sale-chance";
    }


    // 开发计划页面
    @GetMapping("/dev-plan")
    public String customerDevPlan() {
        return "sale/dev-plan";
    }


    // 开发计划信息页面
    @GetMapping("/dev-plan/info/{id}")
    public String devPlan(@PathVariable Integer id, ModelMap mp) {
        mp.addAttribute("saleChance", saleChanceService.findById(id));
        return "sale/dev-plan-info";
    }


    // 客户信息页面
    @GetMapping("/customer")
    public String customer() {
        return "customer/customer";
    }


    // 客户联系人页面
    @GetMapping("/linkage/{id}")
    public String linkage(@PathVariable Integer id, ModelMap mp) {
        mp.addAttribute("customer", customerService.findById(id));
        return "customer/customer-linkage";
    }


    // 客户交往记录页面
    @GetMapping("/contact/{id}")
    public String contact(@PathVariable Integer id, ModelMap mp) {
        mp.addAttribute("customer", customerService.findById(id));
        return "customer/customer-contact";
    }


    // 客户订单信息页面
    @GetMapping("/order/{id}")
    public String order(@PathVariable Integer id, ModelMap mp) {
        mp.addAttribute("customer", customerService.findById(id));
        return "customer/customer-order";
    }


    // 客户订单详情页面
    @GetMapping("/order-detail/{id}")
    public String detail(@PathVariable Integer id, ModelMap mp) {
        mp.addAttribute("order", orderService.findById(id));
        return "customer/customer-order-detail";
    }


    // 客户流失页面
    @GetMapping("/customer-loss")
    public String customerLoss() {
        return "customer/customer-loss";
    }


    // 客户流失措施页面
    @GetMapping("/retrieval/{id}")
    public String retrieval(@PathVariable Integer id, ModelMap mp) {
        mp.addAttribute("loss", lossService.findById(id));
        return "customer/customer-loss-retrieval";
    }


    // 客户服务创建页面
    @GetMapping("/service-create")
    public String customerServiceAdd() {
        return "service/service-create";
    }


    // 客户服务分配页面
    @GetMapping("/service-assign")
    public String customerServiceAssign() {
        return "service/service-assign";
    }


    // 客户服务处理页面
    @GetMapping("/service-process")
    public String customerServiceProcess() {
        return "service/service-process";
    }


    // 客户服务反馈页面
    @GetMapping("/service-feedback")
    public String customerServiceFeedback() {
        return "service/service-feedback";
    }


    // 客户服务归档页面
    @GetMapping("/service-archive")
    public String customerServiceArchive() {
        return "service/service-archive";
    }


    // 客户客户贡献分析
    @GetMapping("/contribution-analysis")
    public String contributionAnalysis() {
        return "report/contribution-analysis";
    }


    // 客户客户构成分析
    @GetMapping("/component-analysis")
    public String componentAnalysis() {
        return "report/component-analysis";
    }


    // 客户客户流失分析
    @GetMapping("/loss-analysis")
    public String lossAnalysis() {
        return "report/loss-analysis";
    }


    // 登录日志
    @GetMapping("/login-history")
    public String loginHistory() {
        return "system/log/login-history";
    }

}
